/*
 * Copyright 2012-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure.cassandra;

import java.time.Duration;
import java.util.List;

import com.datastax.oss.driver.api.core.DefaultConsistencyLevel;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.Resource;

/**
 * Cassandra 的配置属性类。
 *
 * @author Julien Dubois
 * @author Phillip Webb
 * @author Mark Paluch
 * @author Stephane Nicoll
 * @since 1.3.0
 */
@ConfigurationProperties(prefix = "spring.data.cassandra")
public class CassandraProperties {

	/**
	 * 使用的配置文件位置。
	 */
	private Resource config;

	/**
	 * 使用的 keyspace 名称。
	 */
	private String keyspaceName;

	/**
	 * Cassandra 会话名称。
	 */
	private String sessionName;

	/**
	 * 集群节点地址，格式为 'host:port'，或简单的 'host'，此时使用配置的端口。
	 */
	private List<String> contactPoints;

	/**
	 * 如果 contact point 未指定端口，则使用该端口。
	 */
	private int port = 9042;

	/**
	 * 被视为“本地”的数据中心。联系点应来自此数据中心。
	 */
	private String localDatacenter;

	/**
	 * 服务器登录用户名。
	 */
	private String username;

	/**
	 * 服务器登录密码。
	 */
	private String password;

	/**
	 * Cassandra 二进制协议支持的压缩方式。
	 */
	private Compression compression;

	/**
	 * 启动时执行的 schema 操作。
	 */
	private String schemaAction = "none";

	/**
	 * 是否启用 SSL 支持。
	 */
	private boolean ssl = false;

	/**
	 * 连接配置。
	 */
	private final Connection connection = new Connection();

	/**
	 * 连接池配置。
	 */
	private final Pool pool = new Pool();

	/**
	 * 请求配置。
	 */
	private final Request request = new Request();

	/**
	 * 控制连接配置。
	 */
	private final Controlconnection controlconnection = new Controlconnection();

	public Resource getConfig() {
		return this.config;
	}

	public void setConfig(Resource config) {
		this.config = config;
	}

	public String getKeyspaceName() {
		return this.keyspaceName;
	}

	public void setKeyspaceName(String keyspaceName) {
		this.keyspaceName = keyspaceName;
	}

	public String getSessionName() {
		return this.sessionName;
	}

	public void setSessionName(String sessionName) {
		this.sessionName = sessionName;
	}

	public List<String> getContactPoints() {
		return this.contactPoints;
	}

	public void setContactPoints(List<String> contactPoints) {
		this.contactPoints = contactPoints;
	}

	public int getPort() {
		return this.port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getLocalDatacenter() {
		return this.localDatacenter;
	}

	public void setLocalDatacenter(String localDatacenter) {
		this.localDatacenter = localDatacenter;
	}

	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Compression getCompression() {
		return this.compression;
	}

	public void setCompression(Compression compression) {
		this.compression = compression;
	}

	public boolean isSsl() {
		return this.ssl;
	}

	public void setSsl(boolean ssl) {
		this.ssl = ssl;
	}

	public String getSchemaAction() {
		return this.schemaAction;
	}

	public void setSchemaAction(String schemaAction) {
		this.schemaAction = schemaAction;
	}

	public Connection getConnection() {
		return this.connection;
	}

	public Pool getPool() {
		return this.pool;
	}

	public Request getRequest() {
		return this.request;
	}

	public Controlconnection getControlconnection() {
		return this.controlconnection;
	}

	public static class Connection {

		/**
		 * 建立驱动连接时的超时时间。
		 */
		private Duration connectTimeout;

		/**
		 * 初始化过程内部查询的超时时间，连接打开后执行。
		 */
		private Duration initQueryTimeout;

		public Duration getConnectTimeout() {
			return this.connectTimeout;
		}

		public void setConnectTimeout(Duration connectTimeout) {
			this.connectTimeout = connectTimeout;
		}

		public Duration getInitQueryTimeout() {
			return this.initQueryTimeout;
		}

		public void setInitQueryTimeout(Duration initQueryTimeout) {
			this.initQueryTimeout = initQueryTimeout;
		}

	}

	public static class Request {

		/**
		 * 驱动等待请求完成的时间。
		 */
		private Duration timeout;

		/**
		 * 查询一致性级别。
		 */
		private DefaultConsistencyLevel consistency;

		/**
		 * 查询串行一致性级别。
		 */
		private DefaultConsistencyLevel serialConsistency;

		/**
		 * 单次网络往返检索的行数。
		 */
		private Integer pageSize;

		private final Throttler throttler = new Throttler();

		public Duration getTimeout() {
			return this.timeout;
		}

		public void setTimeout(Duration timeout) {
			this.timeout = timeout;
		}

		public DefaultConsistencyLevel getConsistency() {
			return this.consistency;
		}

		public void setConsistency(DefaultConsistencyLevel consistency) {
			this.consistency = consistency;
		}

		public DefaultConsistencyLevel getSerialConsistency() {
			return this.serialConsistency;
		}

		public void setSerialConsistency(DefaultConsistencyLevel serialConsistency) {
			this.serialConsistency = serialConsistency;
		}

		public Integer getPageSize() {
			return this.pageSize;
		}

		public void setPageSize(int pageSize) {
			this.pageSize = pageSize;
		}

		public Throttler getThrottler() {
			return this.throttler;
		}

	}

	/**
	 * 连接池配置属性。
	 */
	public static class Pool {

		/**
		 * 空闲连接被移除前的空闲超时时间。
		 */
		private Duration idleTimeout;

		/**
		 * 空闲连接发送心跳消息的间隔，确保连接存活。
		 */
		private Duration heartbeatInterval;

		public Duration getIdleTimeout() {
			return this.idleTimeout;
		}

		public void setIdleTimeout(Duration idleTimeout) {
			this.idleTimeout = idleTimeout;
		}

		public Duration getHeartbeatInterval() {
			return this.heartbeatInterval;
		}

		public void setHeartbeatInterval(Duration heartbeatInterval) {
			this.heartbeatInterval = heartbeatInterval;
		}

	}

	public static class Controlconnection {

		/**
		 * 控制查询的超时时间。
		 */
		private Duration timeout;

		public Duration getTimeout() {
			return this.timeout;
		}

		public void setTimeout(Duration timeout) {
			this.timeout = timeout;
		}

	}

	public static class Throttler {

		/**
		 * 请求节流类型。
		 */
		private ThrottlerType type;

		/**
		 * 超过阈值时，可排队的最大请求数。
		 */
		private Integer maxQueueSize;

		/**
		 * 并行执行的最大请求数。
		 */
		private Integer maxConcurrentRequests;

		/**
		 * 最大允许的请求速率。
		 */
		private Integer maxRequestsPerSecond;

		/**
		 * 节流器尝试出队请求的间隔。应设置足够高以一次处理多个请求，同时避免请求延迟过长。
		 */
		private Duration drainInterval;

		public ThrottlerType getType() {
			return this.type;
		}

		public void setType(ThrottlerType type) {
			this.type = type;
		}

		public Integer getMaxQueueSize() {
			return this.maxQueueSize;
		}

		public void setMaxQueueSize(int maxQueueSize) {
			this.maxQueueSize = maxQueueSize;
		}

		public Integer getMaxConcurrentRequests() {
			return this.maxConcurrentRequests;
		}

		public void setMaxConcurrentRequests(int maxConcurrentRequests) {
			this.maxConcurrentRequests = maxConcurrentRequests;
		}

		public Integer getMaxRequestsPerSecond() {
			return this.maxRequestsPerSecond;
		}

		public void setMaxRequestsPerSecond(int maxRequestsPerSecond) {
			this.maxRequestsPerSecond = maxRequestsPerSecond;
		}

		public Duration getDrainInterval() {
			return this.drainInterval;
		}

		public void setDrainInterval(Duration drainInterval) {
			this.drainInterval = drainInterval;
		}

	}

	/**
	 * 协议帧压缩算法名称。
	 */
	public enum Compression {

		/**
		 * 需要依赖 'net.jpountz.lz4:lz4'。
		 */
		LZ4,

		/**
		 * 需要依赖 org.xerial.snappy:snappy-java。
		 */
		SNAPPY,

		/**
		 * 不压缩。
		 */
		NONE;

	}

	public enum ThrottlerType {

		/**
		 * 限制并行执行的请求数量。
		 */
		CONCURRENCY_LIMITING("ConcurrencyLimitingRequestThrottler"),

		/**
		 * 限制每秒请求速率。
		 */
		RATE_LIMITING("RateLimitingRequestThrottler"),

		/**
		 * 不进行请求节流。
		 */
		NONE("PassThroughRequestThrottler");

		private final String type;

		ThrottlerType(String type) {
			this.type = type;
		}

		public String type() {
			return this.type;
		}

	}

}
